<?php
require_once __DIR__."/Base_model.php";
class Host_material_model extends Base_model
{
    public function __construct()
    {
        parent::__construct();
        $this->table = "video";
        $this->table1 = "type";
        $this->table2 = "type_sub";
    }

    /**
     * 原始和弃用主页查询
     */
    public function all($arr){
        $this->db->select('*');
        $this->db->from($this->table);
        //查询为未删除视频
        $this->db->where("status",2);
        if($arr['type']==2){
            //原始素材
            $this->db->where("video_type",2);
        }else if($arr['type']==1){
            //弃用视频
            $this->db->where("video_type",1);
        }
        //话题筛选
        if(!empty($arr['join_type_sub'])){
            $this->db->where("video_join_sub",$arr['join_type_sub']);
        }
        //时间筛选
        if(!empty($arr['start_time']) && !empty($arr['end_time'])){
            $this->db->where("add_time >=",$arr['start_time']);
            $this->db->where("add_time <",$arr['end_time']);
        }
        //分页
        if(!empty($arr['page']) && !empty($arr['limit'])){
            $pages=($arr['page']-1)*$arr['limit'];
            $datas['total']=$this->db->count_all_results("",false);
            $this->db->limit($arr['limit'],$pages);
        }else{
            $datas['total']=$this->db->count_all_results("",false);
        }
        //排序
//        if($arr['type']==2){
//            //原始素材
//            $this->db->order_by('add_time',"desc");
//        }else if($arr['type']==1){
//            //弃用视频
//            $this->db->order_by('drop_time',"desc");
//        }
        $this->db->order_by('add_time',"desc");
        $res = $this->db->get();
        $datas['row']= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 已挑选主页查询
     */
    public function join($arr){
        $this->db->select('*');
        $this->db->from($this->table);
        //查询为未删除视频
        $this->db->where("status",2);
        $this->db->where("video_type",3);
        //话题筛选
        if(!empty($arr['join_type_sub'])){
            $this->db->where("video_join_sub",$arr['join_type_sub']);
        }
        //时间筛选
        if(!empty($arr['start_time']) && !empty($arr['end_time'])){
            $this->db->where("add_time >=",$arr['start_time']);
            $this->db->where("add_time <",$arr['end_time']);
        }
        //分页
        if(!empty($arr['page']) && !empty($arr['limit'])){
            $pages=($arr['page']-1)*$arr['limit'];
            $datas['total']=$this->db->count_all_results("",false);
            $this->db->limit($arr['limit'],$pages);
        }else{
            $datas['total']=$this->db->count_all_results("",false);
        }
        //排序
        if(!empty($arr['sort']) && $arr['sort']==1){
            $this->db->order_by("sorts","desc");
            $this->db->order_by('add_time',"desc");
        }else if(!empty($arr['dub_num']) && $arr['dub_num']==1){
            $this->db->order_by('(dub_num-0)',"desc");
            $this->db->order_by('add_time',"desc");
        }else if(!empty($arr['times']) && $arr['times']==1){
            $this->db->order_by('join_time',"desc");
        }else{
            $this->db->order_by('sorts',"desc");
            $this->db->order_by('add_time',"desc");
        }

        $res = $this->db->get();
        $datas['row']= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 修改排序
     */

    public function update_sort($sort,$id,$old_sort){
        //1.先去查询所有排序为1-999的
        $this->db->select("video_uuid,sorts");
        $this->db->from($this->table);
        $this->db->where("sorts>=",1);
        $this->db->where("sorts<=",999);
        $this->db->order_by('sorts',"asc");
        $res1= $this->db->get();
        $data1 =$res1->result_array();
        if(!empty($data1)){
            //4.如果排序值小于已排序个数，把小于这个排序值的放入一个数组，从一开始依次排，大于这个排序的，放入另一个数组，从这个排序之后一次排
            $sort1 = [];
            $sort2 = [];
            $data2 = [];
            //4.原有排序值大于这个排序值的放入$sort2，小于的放入$sort1
            foreach ($data1 as $k => $v) {
                if ($v['sorts'] < $sort && $v['sorts'] != $old_sort) {
                    $sort1[] = $v['sorts'];
                } else if ($v['sorts'] >= $sort && $v['sorts'] != $old_sort) {
                    $sort2[] = $v['sorts'];
                } else {
                    //5.删除$data1中的值，方便遍历，用$data2遍历就行
                    unset($data1[$k]);
                }
            }
            foreach ($data1 as $a => $b) {
                $data2[] = $b;
            }
            $g1 = $this->s1($sort1);
            $g2 = $this->s2($sort2, $sort);
            $g3 = array_merge($g1, $g2);
            $sort3 = array_merge($sort1, $sort2);

            ///////////
            if (empty(array_diff_assoc($g3, $sort3))) {
                $res = $this->update($this->table, ['sorts' => $sort], ['video_uuid' => $id]);
                if ($res) {
                    return true;
                } else {
                    return false;
                }

            } else {
                //先去把以前的排序值从1开始依次排序
                foreach ($data2 as $kk => $vv) {
                    $this->update($this->table, ['sorts' => $g3[$kk]], ['video_uuid' => $vv['video_uuid']]);
                }
                //再去修改这个要排序的值
                $res = $this->update($this->table, ['sorts' => $sort], ['video_uuid' => $id]);
                if ($res) {
                    return true;
                } else {
                    return false;
                }
            }
        }else{
            $res=$this->update($this->table,['sorts'=>$sort],['video_uuid'=>$id]);
            if($res){
                return true;
            }else{
                return false;
            }
        }


    }

    public function s1($sort){
        if(!empty($sort)){
            $sort[0]=1;
            $len=count($sort);
            for($i=1;$i<$len;$i++){
                $sort[$i]=$sort[$i-1]+1;
            }
            return $sort;
        }else{
            return [];
        }
    }
    public function s2($sort1,$sort){
        if(!empty($sort1)){
            $sort1[0]=$sort+1;
            $len=count($sort1);
            for($i=1;$i<$len;$i++){
                $sort1[$i]=$sort1[$i-1]+1;
            }
            return $sort1;
        }else{
            return [];
        }
    }






//    public function update_sort($sort,$video_uuid){
//            //有这个排序
//            if($sort==999999999){
//                return $this->update_sort_999($sort,$video_uuid);
//            }else if($sort==100){
//                return $this->update_sort_100($sort,$video_uuid);
//            }else if($sort<100){
//                return  $this->update_sort_10($sort,$video_uuid);
//            }
//    }
//
//    public function gaibian_sort($arr){
//        if(!is_array($arr)){
//            return false;
//        }
//        if($arr[0]!=1){
//            $arr[0]=1;
//        }
//        $length=count($arr);
//        for($i=0;$i<$length;$i++){
//            if($i==$length-1){
//                $arr[$length-1]=$arr[$length-2]+1;
//            }else{
//                if($arr[$i+1]-$arr[$i]!=1){
//                    $arr[$i+1]=$arr[$i]+1;
//                }
//            }
//        }
//        return $arr;
//
//    }
//
//    /**
//     * 修改排序为999999999的排序
//     */
//    public function update_sort_999($sort,$video_uuid){
//        //1.先去改变要改变的值
//        $this->db->update($this->table,['sorts'=>$sort],['video_uuid'=>$video_uuid]);
//        //2.先去查询排序为1-100的
//        $this->db->select('video_id,sorts');
//        $this->db->from($this->table);
//        $this->db->where("sorts >",0);
//        $this->db->where("sorts <",100);
//        $this->db->order_by("sorts","asc");
//        $res1 = $this->db->get();
//        $data1= $res1->result_array();
//        $sorts=[];
//        if(!empty($data1)){
//            foreach($data1 as $k=>$v){
//                $sorts[]=$v['sorts'];
//            }
//            $gaibian=$this->gaibian_sort($sorts);
//            foreach($data1 as $kk=>$vv){
//                $this->db->update($this->table,['sorts'=>$gaibian[$kk]],['video_id'=>$vv['video_id']]);
//            }
//            return "ok";
//        }else{
//            return "ok";
//        }
//    }
//
//    /**
//     * 修改排序为100的排序
//     */
//    public function update_sort_100($sort,$video_uuid){
//        //1.先去查询以前有没有100的排序，
//        $this->db->select('video_id,sorts');
//        $this->db->from($this->table);
//        $this->db->where("sorts",100);
//        $res1 = $this->db->get();
//        $data1= $res1->row_array();
//        //如果有这个排序变为999999999，没有直接添加
//        if($data1){
//            $this->db->trans_begin();// 开启事务
//            $this->update($this->table,['sorts'=>'999999999'],["video_id"=>$data1['video_id']]);
//
//            $arr=$this->db->affected_rows();//受影响的行数
//            $this->db->update($this->table,['sorts'=>100],["video_uuid"=>$video_uuid]);
//            if($this->db->trans_status()===false || $arr==0){
//                $this->db->trans_rollback();//回滚
//                return "修改100排序失败";
//            }else{
//                $this->db->trans_commit();//提交事物
//                return "ok";
//            }
//        }else{
//            $res=$this->update($this->table,['sorts'=>100],["video_uuid"=>$video_uuid]);
//            if($res){
//                return "ok";
//            }else{
//                return "100排序失败";
//            }
//        }
//    }
//
//    /**
//     * 修改排序为0-100的排序
//     */
//    public function update_sort_10($sort,$video_uuid){
//        //1.0-100之间的排序先去查询是否有这个排序
//        $this->db->select('video_id,sorts');
//        $this->db->from($this->table);
//        $this->db->where("sorts",$sort);
//        $res1 = $this->db->get();
//        $data1= $res1->row_array();
//        //
//        if(empty($data1)){
//            //2.如果为空就直接插入这个排序，并把后面的排序依次排列
//            $this->update($this->table,['sorts'=>$sort],["video_uuid"=>$video_uuid]);
//            //然后再去查比这个排序大的排序，并依次排序
//            $this->db->select('video_id,sorts');
//            $this->db->from($this->table);
//            $this->db->where('sorts >',$sort);
//            $this->db->where('sorts <=',100);
//            $this->db->order_by('sorts',"asc");
//            $res2 = $this->db->get();
//            $data2= $res2->result_array();
//            $sorts=[];
//            if(!empty($data2)){
//                foreach($data2 as $k=>$v){
//                    $sorts[]=$v['sorts'];
//                }
//            }
//            $gaibian1=$this->gaibian1($sorts,$sort);
//            foreach($data2 as $kk=>$vv){
//                $this->db->update($this->table,['sorts'=>$gaibian1[$kk]],['video_id'=>$vv['video_id']]);
//            }
//            return "ok";
//
//        }else{
//           //3.如果不为空，先把这个100的变为999999999
//            $this->db->select('video_id,sorts');
//            $this->db->from($this->table);
//            $this->db->where('sorts',100);
//            $res3 = $this->db->get();
//            $data3= $res3->row_array();
//            if(!empty($data3)){
//                $this->update($this->table,['sorts'=>999999999],['video_id'=>$data3['video_id']]);
//            }
//            //4.再去查出要改变的这个值的排序
//            $this->db->select('video_id,sorts');
//            $this->db->from($this->table);
//            $this->db->where('video_uuid',$video_uuid);
//            $res7 = $this->db->get();
//            $data7= $res7->row_array();
//            //5.再去把比这个数大的抖加一，要排除改变的这个值
//            $this->db->select('video_id,sorts');
//            $this->db->from($this->table);
//            $this->db->where('sorts >=',$sort);
//            $this->db->where('sorts <=',100);
//            $res4 = $this->db->get();
//            $data4= $res4->result_array();
//            if(!empty($data4)){
//                foreach($data4 as $k=>$v){
//                    $s1=$v['sorts']+1;
//                    $this->update($this->table,['sorts'=>$s1],['video_id'=>$v['video_id']]);
//                }
//            }
//            //6.再去改变这个值
//            $this->update($this->table,['sorts'=>$sort],["video_uuid"=>$video_uuid]);
//            //7.再把比这个值大的依次排序
//            $this->db->select('video_id,sorts');
//            $this->db->from($this->table);
//            $this->db->where('sorts >',$sort);
//            $this->db->where('sorts <=',100);
//            $this->db->order_by('sorts',"asc");
//            $res5 = $this->db->get();
//            $data5= $res5->result_array();
//            $sorts=[];
//            if(!empty($data5)){
//                foreach($data5 as $kk=>$vv){
//                    $sorts[]=$vv['sorts'];
//                }
//            }
//            $gaibian1=$this->gaibian1($sorts,$sort);
////            return $gaibian1;
//            foreach($data5 as $kkk=>$vvv){
//                $this->db->update($this->table,['sorts'=>$gaibian1[$kkk]],['video_id'=>$vvv['video_id']]);
//            }
//            return "ok";
//        }
//    }
//
//    public function gaibian1($arr,$sort){
//        if($arr[0]-$sort !=1){
//            $arr[0]=$sort+1;
//        }
//        $length=count($arr);
//        for($i=0;$i<$length;$i++){
//            if($i==$length-1){
//                $arr[$length-1]=$arr[$length-2]+1;
//            }else{
//                if($arr[$i+1]-$arr[$i]!=1){
//                    $arr[$i+1]=$arr[$i]+1;
//                }
//            }
//        }
//        return $arr;
//    }













    public function update_sort1($sort,$video_uuid){
        $this->db->select('video_id,sorts,video_uuid');
        $this->db->from($this->table);
        //查询为未删除视频
        $this->db->where("sorts",$sort);
        $res = $this->db->get();
        $datas= $res->row_array();
        if(empty($datas)){
            //1.没有这个排序
            return $this->update($this->table,['sorts'=>$sort],['video_uuid'=>$video_uuid]);
        }else{
            //有这个排序
            if($sort==999999999){
                //1.排序值为0
                return $this->update($this->table,['sorts'=>$sort],['video_uuid'=>$video_uuid]);
            }else if($sort==100){
                //2.排序值为100的情况
                $this->db->select('video_id,sorts,video_uuid');
                $this->db->from($this->table);
                //查询为未删除视频
                $this->db->where("sorts",$sort);
                $res = $this->db->get();
                $data= $res->row_array();
                if(!empty($data)){
                    $this->db->trans_begin();// 开启事务
                    //如果有排序值为100的，先把这个排序值变为999999999，再去修改这个排序值为100
                    $this->update($this->table,['sorts'=>999999999],['video_uuid'=>$data['video_uuid']]);
                    //受影响的行数
                    $arr=$this->db->affected_rows();
                    $this->update($this->table,['sorts'=>$sort],['video_uuid'=>$video_uuid]);
                    if($this->db->trans_status()===false || $arr==0){
                        $this->db->trans_rollback();//回滚
                        return false;
                    }else{
                        $this->db->trans_commit();//提交事物

                        return true;
                    }
                }else{
                    return $this->update($this->table,['sorts'=>$sort],['video_uuid'=>$video_uuid]);
                }
            }else if($sort<100){
                //3.排序值小于100的情况
                $this->db->select('video_id,sorts,video_uuid');
                $this->db->from($this->table);
                //查询为未删除视频
                $this->db->where("sorts>=",$sort);
                $this->db->where("sorts<=",100);
                $res = $this->db->get();
                $data1= $res->result_array();
                $this->db->trans_begin();// 开启事务
                foreach($data1 as $k=>$v){
                    $sorts=$v['sorts']+1;
                    if($sorts>100){
                        $sorts=999999999;
                    }
                    $this->update($this->table,['sorts'=>$sorts],['video_uuid'=>$v['video_uuid']]);
                    //受影响的行数
                    $arr=$this->db->affected_rows();
                }
               $this->update($this->table,['sorts'=>$sort],['video_uuid'=>$video_uuid]);
                if($this->db->trans_status()===false || $arr==0){
                    $this->db->trans_rollback();//回滚
                    return false;
                }else{
                    $this->db->trans_commit();//提交事物

                    return true;
                }
            }
        }
    }


    /**
     * 查询分类
     */
    public function types($type=1){
        $this->db->select('*');
        $this->db->from($this->table1);
        if($type==1){
            $this->db->where("is_other !=",999999999);
            $this->db->where("online",2);
        }
        $res = $this->db->get();
        $datas= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 查询话题
     */
    public function type_sub($where=[],$type=1){
        $this->db->select('*');
        $this->db->from($this->table2);
        if($type!=3){
            $this->db->where("is_other!=",999999999);
        }

        //这是不加入已挑选视频
        if($type==1){
            $this->db->where("online",2);
            $this->db->where($where);
        }
        $res = $this->db->get();
        $datas= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }
}